﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using System.Drawing;
using System.IO;
using ExtBusiness.FlowOperation;


public partial class WebPubManager_WorkFlowView :PageBase
{
    protected WorkFlowPubSettting wfpsTemp = new WorkFlowPubSettting();
    protected string strWid = string.Empty;//流程WID
    protected string strFlowName = string.Empty;//流程名称
    protected string strFlowValue = string.Empty;//流程显示类型值

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDefaultValues();
        }
    }

    /// <summary>
    /// 初始化值
    /// </summary>
    /// <!--addby zhongjian 20100426-->
    protected void BindDefaultValues()
    {
        ViewState["FlowName"] = Request.QueryString["datatype"];
        ViewState["flowid"] = Request.QueryString["flowid"];
        DataTable dtTemp = wfpsTemp.GetWorkFlowView(ViewState["flowid"].ToString());
        if (dtTemp.Rows.Count > 0)
        {
            string strFlag = dtTemp.Rows[0]["isshowdialog"].ToString();
            if (strFlag == "0")
            {
                cbShowType.Checked = false;
            }
            else
            {
                cbShowType.Checked = true;
            }
            ViewState["FlagView"] = dtTemp.Rows[0]["flagview"].ToString();
            rblFlow.SelectedValue = ViewState["FlagView"].ToString();
            strFlowValue = ViewState["FlagView"].ToString();
            switch (strFlowValue)
            {
                case "0"://审批流程
                    ViewState["FlowName"] = Request.QueryString["datatype"];
                    cbShowType.Visible = true;
                    lblShow.Visible = true;
                    lblShow1.Visible = false;
                    BtnUpLoad.Visible = false;
                    FileUpload1.Visible = false;
                    break;
                case "1"://简易流程
                    ViewState["FlowName"] = Request.QueryString["datatype"] + "(简)";
                    cbShowType.Visible = true;
                    lblShow.Visible = true;
                    lblShow1.Visible = false;
                    BtnUpLoad.Visible = false;
                    FileUpload1.Visible = false;
                    break;
                case "2"://上传流程
                    cbShowType.Visible = false;
                    lblShow.Visible = false;
                    lblShow1.Visible = true;
                    BtnUpLoad.Visible = true;
                    FileUpload1.Visible = true;
                    break;
                default:
                    break;
            }
        }
    }

    /// <summary>
    /// 绑定显示办理流程图
    /// </summary>
    /// <!--addby zhongjian 20091209-->
    public string ShowWorkFlow()
    {
        string strHttp = string.Empty;
        strFlowValue = ViewState["FlagView"].ToString();
        if (strFlowValue == "0" || strFlowValue == "1")
        {
            strFlowName = ViewState["FlowName"].ToString();
            strWid = wfpsTemp.GetWorkWid(strFlowName);
            Common.ConfigHelper config = new Common.ConfigHelper(true);
            String strNewActivexServer = config.GetAppSettingsValue("ActivexDisplay");

            strHttp = string.Format("../..{0}?wid={1}&IsShowDialog=1", strNewActivexServer, strWid);
        }
        else
        {
            strHttp = "WorkViewUpLoad.aspx?flowid="+Request.QueryString["flowid"].ToString();
        }
        return strHttp;
    }

    /// <summary>
    /// 保存流程是否显示办理人员的操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string strFlag = "0";
        if (cbShowType.Checked)
            strFlag = "1";
        wfpsTemp.SetShowDialog(ViewState["flowid"].ToString(), strFlag, ViewState["FlagView"].ToString());
    }

    protected void rblFlow_SelectedIndexChanged(object sender, EventArgs e)
    {
        ViewState["FlagView"] = rblFlow.SelectedValue;
        strFlowValue = ViewState["FlagView"].ToString();
        switch(strFlowValue)
        {
            case "0"://审批流程
                ViewState["FlowName"] = Request.QueryString["datatype"];
                cbShowType.Visible = true;
                lblShow.Visible = true;
                lblShow1.Visible = false;
                BtnUpLoad.Visible = false;
                FileUpload1.Visible = false;
                break;
            case "1"://简易流程
                ViewState["FlowName"] = Request.QueryString["datatype"] + "(简)";
                cbShowType.Visible = true;
                lblShow.Visible = true;
                lblShow1.Visible = false;
                BtnUpLoad.Visible = false;
                FileUpload1.Visible = false;
                break;
            case "2"://上传流程
                cbShowType.Visible = false;
                lblShow.Visible = false;
                lblShow1.Visible = true;
                BtnUpLoad.Visible = true;
                FileUpload1.Visible = true;
                break;
            default:                
                break;
        }
    }

    protected void BtnUpLoad_Click(object sender, EventArgs e)
    {
        string strMsg = string.Empty;
        string fileName = string.Empty;
        string fileType = string.Empty;
        try
        {
            if (FileUpload1.HasFile)
            {
                fileName = System.IO.Path.GetFileName(FileUpload1.FileName);
                fileType = System.IO.Path.GetExtension(fileName);
                //验证文件类型
                fileType = fileType.Replace(".", "").ToLower();
                if (fileType == "jpg" || fileType == "png" || fileType == "gif" || fileType == "jpeg" || fileType == "bmp")//jpg,png,gif,jpeg,bmp
                {
                    //取得上传文件的大小
                    int FileLen = FileUpload1.PostedFile.ContentLength;
                    if (FileLen > 1024 * 1024 * 3)
                    {
                        strMsg = "图片大小不能大于3M！";
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "", string.Format(" <script> alert('{0}'); </script> ", strMsg));
                    }
                    else
                    {
                        Byte[] FileData = new Byte[FileLen];

                        //创建访问客户端上传文件的对象
                        HttpPostedFile hp = FileUpload1.PostedFile;

                        //创建数据流对象
                        Stream sr = hp.InputStream;

                        //将图片数据放到FileData数组对象实例中，0代表数组指针的起始位置,FileLen代表指针的结束位置
                        sr.Read(FileData, 0, FileLen);

                        wfpsTemp.SaveWorkFlowView(ViewState["flowid"].ToString(), FileData);
                    }
                }
                else
                {
                    strMsg = "流程图格式支持jpg,png,gif,jpeg,bmp格式的图片！";
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", string.Format(" <script> alert('{0}'); </script> ", strMsg));
                }
            }
            else
            {
                strMsg = "请选择你要上传的流程图。";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", string.Format(" <script> alert('{0}'); </script> ", strMsg));
            }

        }
        catch (Exception ex)
        {
            this.Response.Write(ex.ToString());
        }
    } 
}
